﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportDetail.aspx.cs" Inherits="CRMWeb.Pages.Report.ReportDetail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
        <div class="ftitle" style="padding-left:344px;">Loan Purpose Analysis</div>
        <div class="fitem">
            <label style="width: 50px;"></label>
           <input id="rdoPeriod" checked="checked" type="radio" name="rdoCommon" />
            <label style="width: 50px;">Period :</label>
           <select id="cmbMonths" style="width:100px" data-options="panelHeight:'auto'" name="Months">
                <option value="1">January</option>
                <option value="2">February</option>
                <option value="3">March</option>
                <option value="4">April</option>
                <option value="5">May</option>
                <option value="6">June</option>
                <option value="7">July</option>
                <option value="8">August</option>
                <option value="9">September</option>
                <option value="10">October</option>
                <option value="11">November</option>
                <option value="12">December</option>
            </select>
           <label style="width: 6px;"></label>
           <select id="cmbYear" style="width:80px" data-options="panelHeight:'auto'" name="chooseYears">
               <option value="2008">2008</option>
               <option value="2009">2009</option>
               <option value="2010">2010</option>
               <option value="2011">2011</option>
               <option value="2012">2012</option>
               <option value="2013">2013</option>
               <option value="2014">2014</option>
               <option value="2015">2015</option>
               <option value="2016">2016</option>
               <option value="2017">2017</option>
               <option value="2018">2018</option>
            </select>
            <label style="width: 11px;"></label>
             <input id="rdoFrom" type="radio" name="rdoCommon" />
            <label style="width:  38px;">From :</label>
            <input id="txtStartDate" name="StartDate" class="easyui-datebox" data-options="formatter:au_formatter,parser:au_parser" type="text"  style="width: 90%;" />
           
           <label style="width: 23px;margin-left:12px">To :</label>
            <input id="txtFinishDate" name="FinishDate" class="easyui-datebox" data-options="formatter:au_formatter,parser:au_parser"  type="text"  style="width: 90%;" />
            &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp
             <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="GetLoanPurposeReport()">Confirm</a>
        </div>

        <div class="fitem" id="reportGrid">
            <table id="dgReport"  title="Report - Application" style="width: auto;" >
                <thead>
                    <tr>
                        <th rowspan="2" field="PurposeSummary" width="120" data-options="halign:'center',align:'left'" ><span style="font-weight:bold">Purpose</span></th>
                        <th rowspan="2" field="TotalNumber" width="80" data-options="halign:'center',align:'center'"  ><span style="font-weight:bold">Total No</span></th>
                        <th rowspan="2" field="Percent" width="100" align="center" data-options="formatter:AddPercentSign"><span style="font-weight:bold">Percentage</span></th>
                        <th rowspan="2" field="AverageLoanAmount" align="right" data-options="formatter:ReplaceNumberWithCommas,halign:'center'" width="200"  ><span style="font-weight:bold">Average Desired Amount</span></th>
                        <th rowspan="2" field="MedianLoanAmount" align="right" data-options="formatter:ReplaceNumberWithCommas,halign:'center'" width="200"  ><span style="font-weight:bold">Medium Desired Amount</span></th>
                    </tr>
                </thead>
            </table>
        </div>
        
    </form>
   
    <script type="text/javascript">

        $(document).ready(function () {
            var today = new Date();
            var currentMonth = today.getMonth();
            var currentYear = today.getFullYear();

            if (currentMonth == 0) {
                currentMonth = 12;
                currentYear -= 1;
            }
            $('#cmbMonths').val(currentMonth);
            $('#cmbYear').val(currentYear);
            GetLoanPurposeReport();
        });

        function GetLoanPurposeReport() {

            if ($('#rdoPeriod').is(':checked')) {
                var selectedMonth = $('#cmbMonths').val();
                var selectedYear = $('#cmbYear').val();

                $('#dgReport').datagrid({
                    url: '/WebServices/Report/Report.ashx?method=GetLoanPurposeReportByDate&selectedMonth=' + selectedMonth + '&selectedYear='+selectedYear
                });
               // $('#dgReport').datagrid('reload');
            }

            if ($('#rdoFrom').is(':checked')) {
                if (GetDateById('txtFinishDate') < GetDateById('txtStartDate')) {
                    $.messager.alert('Morgan CRM Report', 'Start date should be less than End date');
                }
                else {
                    var fromDate = $('#txtStartDate').datebox('getValue');
                    var toDate = $('#txtFinishDate').datebox('getValue');
                    $('#dgReport').datagrid({
                        url: '/WebServices/Report/Report.ashx?method=GetLoanPurposeReportByPeriod&fromDate=' + fromDate + '&toDate=' + toDate
                    });
                    $('#dgReport').datagrid('reload');
                }
            }
        }

        function GetDateById(id) {
            var currentDate = $('#' + id).datebox('getValue').split('/');
            var d = new Number(currentDate[0]);
            var m = new Number(currentDate[1]);
            var y = new Number(currentDate[2]);
            return new Date(y, m - 1, d);
        }
        function ReplaceNumberWithCommas(yourNumber) {
            if (yourNumber != null) {
                var components = yourNumber.toString().split(".");
                components[0] = components[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                return "$" + components.join(".");
            }
        }
        function AddPercentSign(percent) {
            return percent + " %";
        }
        function au_formatter(value, row) {
            var date = (value === null) ? new Date() : new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }
        function au_parser(s) {
            if (!s) return new Date();
            var ss = (s.split('/'));
            var d = new Number(ss[0]);
            var m = new Number(ss[1]);
            var y = new Number(ss[2]);
            var currentDate;
            if (!isNaN(d) && !isNaN(m) && !isNaN(y)) {

                return new Date(y, m - 1, d);
            }
            else {
                return new Date();
            }
        }
      
    </script>   
</body>
</html>
